package com.acando.mate.business;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;

public class WorkerProvider {

	public WorkerProvider() {

	}

	private class WorkerDownloader extends AsyncTask<String, String, String> {
		InputStream inputStream = null;

		@Override
		protected String doInBackground(String... params) {
			String result = "";
			String url_string = "http://yoururlhere.com";

			ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
			HttpURLConnection connection = null;
			InputStream input = null;
			URL url;
			try {
				url = new URL(url_string);
				connection = (HttpURLConnection) url.openConnection();
				connection.connect();

				if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
					return "Server returned HTTP "
							+ connection.getResponseCode() + " "
							+ connection.getResponseMessage();
				}

				input = connection.getInputStream();
			} catch (MalformedURLException e1) {
				Log.e("MalformedURLException", e1.toString());
				e1.printStackTrace();
			} catch (IOException e2) {
				Log.e("IOException", e2.toString());
				e2.printStackTrace();
			}
			// Convert response to string using String Builder
			try {
				BufferedReader bReader = new BufferedReader(
						new InputStreamReader(inputStream, "iso-8859-1"), 8);
				StringBuilder sBuilder = new StringBuilder();

				String line = null;
				while ((line = bReader.readLine()) != null) {
					sBuilder.append(line + "\n");
				}

				inputStream.close();
				result = sBuilder.toString();

			} catch (Exception e) {
				Log.e("StringBuilding & BufferedReader",
						"Error converting result " + e.toString());
			}
			return result;
		}

		protected void onPostExecute(String result) {
			// parse JSON data
			try {
				JSONArray jArray = new JSONArray(result);
				for (int i = 0; i < jArray.length(); i++) {

					JSONObject jObject = jArray.getJSONObject(i);

					String name = jObject.getString("name");
					String tab1_text = jObject.getString("tab1_text");
					int active = jObject.getInt("active");

				}
			} catch (JSONException e) {
				Log.e("JSONException", "Error: " + e.toString());
			}
		}
	}

}
